package id.khairizalnetani.tugasakhir;

import id.khairizalnetani.tugasakhir.entity.Laporan;
import id.khairizalnetani.tugasakhir.entity.Siswa;
import java.util.ArrayList;
import java.util.List;
import roboguice.activity.RoboActivity;
import roboguice.inject.InjectView;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class HalamanLaporanActivity extends RoboActivity implements OnClickListener, OnItemClickListener
{
	@InjectView(R.id.empty)
	TextView					txtEmpty;
	@InjectView(R.id.listviewlaporan)
	ListView					lvLaporan;

	@InjectView(R.id.btnLaporanKeluar)
	Button						btnKeluar;
	@InjectView(R.id.btnLaporanKembali)
	Button						btnKembali;

	private static final int	TXT_APEL_PAGI	= 0;
	private static final int	TXT_APEL_SIANG	= 1;
	private static final int	TXT_APEL_MALAM	= 2;

	private DBAdapter			db;
	private ProgressDialog		pDialog;
	private Siswa				siswa;
	private int					idSiswa;
	private List<Laporan>		listLaporan;
	private List<String>		listString;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_halaman_laporan);

		// instance kelas db adapter
		db = new DBAdapter(this);
		db.open();

		// ambil data dari activity sebelumnya
		Bundle bundle = getIntent().getExtras();
		if (bundle != null)
		{
			// ambil data id siswa
			idSiswa = bundle.getInt(Halaman1.TAG_ID);
		}

		lvLaporan.setEmptyView(txtEmpty);
		lvLaporan.setOnItemClickListener(this);
		btnKeluar.setOnClickListener(this);
		btnKembali.setOnClickListener(this);

		// jalankan asynctask
		new AsyncTaskLaporan().execute();
	}

	// tampilkan data ke listview berupa laporan yang ada dari database
	private void tampilDataKeListview()
	{
		if (listString == null)
			listString = new ArrayList<String>();

		for (int i = 0; i < listLaporan.size(); i++)
		{
			listString.add(getStringApel(listLaporan.get(i).getJenis_apel()) +
					" - " + listLaporan.get(i).getTanggal());
		}

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listString);
		lvLaporan.setAdapter(adapter);
	}

	// ambil string apel
	private String getStringApel(int jenisApel)
	{
		String teks = "";
		switch (jenisApel)
		{
			case TXT_APEL_PAGI:
				teks = "Apel Pagi";
				break;
			case TXT_APEL_SIANG:
				teks = "Apel Siang";
				break;
			case TXT_APEL_MALAM:
				teks = "Apel Malam";
				break;
		}
		return teks;
	}

	private class AsyncTaskLaporan extends AsyncTask<Void, Void, Void>
	{
		@Override
		protected Void doInBackground(Void... params)
		{
			// TODO Auto-generated method stub
			listLaporan = new ArrayList<Laporan>();
			listLaporan = db.getAllReport();
			siswa = db.getSiswaById(idSiswa);
			return null;
		}

		@Override
		protected void onPostExecute(Void result)
		{
			// TODO Auto-generated method stub

			pDialog.dismiss();
			if (listLaporan != null)
			{
				tampilDataKeListview();
			} else
			{
				Utils.TRACE("List kosong");
			}

			super.onPostExecute(result);
		}

		@Override
		protected void onPreExecute()
		{
			// TODO Auto-generated method stub
			super.onPreExecute();

			pDialog = new ProgressDialog(HalamanLaporanActivity.this);
			pDialog.setMessage("Load Data...");
			pDialog.setCancelable(true);
			pDialog.show();
		}
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.btnLaporanKeluar:
				finish();
				break;
			case R.id.btnLaporanKembali:

				Bundle b = new Bundle();
				b.putInt(Halaman1.TAG_ID, siswa.getId());
				b.putString(Halaman1.TAG_NAMA, siswa.getNama());
				b.putString(Halaman1.TAG_KODE, siswa.getKode_aktivasi());
				b.putInt(Halaman1.TAG_SKADRON, siswa.getSkadron());
				b.putInt(Halaman1.TAG_FLIGHT, siswa.getFlight());

				Intent i = new Intent(this, SelamatDatangActivity.class);
				i.putExtras(b);
				startActivity(i);
				finish();
				db.close();
				break;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int location, long arg3)
	{
		Bundle bundle = new Bundle();
		Utils.TRACE("id siswa di hal laporan " + siswa.getId());
		bundle.putInt(Halaman1.TAG_ID, siswa.getId());
		bundle.putLong("id_report", listLaporan.get(location).getId_report());

		Intent i = new Intent(HalamanLaporanActivity.this, LihatLaporan.class);
		i.putExtras(bundle);
		startActivity(i);
		finish();
	}

}
